class Graph
  
  def initialize(nodes=[])
    @nodes=nodes;
  end
  
  def dfs(id)
    open_all
    node = get_node(id)
    node.dfs
    print "\n"
  end
  
  def bfs(id)
    open_all
    que = []
    start_node = get_node(id)
    que.push(start_node)
    
    while !que.empty? do
      n = que.first
      print n.id
      print " "
      que.delete(n);
      n.flag = :closed
      opened_neighbors = n.neighbors.select { |x| x.flag == :opened }
      if opened_neighbors != nil
        opened_neighbors.each { |x| que.push(x) }
      end
    end
    print "\n"
  end
  
  def get_node(id)
    n = @nodes.select {|x| x.id==id}
    if n == nil
      return nil;
    else
      return n[0]
    end
  end
  
  def add_node(id)
    n=nil
    if get_node(id)==nil
      n = Node.new(id)
      @nodes << n
      return n
    else
      return get_node(id)
    end
    
  end
  
  def open_all
    @nodes.each { |x| x.flag = :opened }
  end
  
  def to_s
    out = "Graph: #{@nodes.length}\n"
    out += @nodes.to_s
    out
  end
  
end
class Node
  attr_accessor(:id,:neighbors,:flag)
  
  def initialize(id)
    @id=id
    @neighbors = [];
    @flag = :opened;
  end
  
  def dfs
    if(@flag != :opened)
      return;
    end
    print "#{@id} "
    @flag=:closed;
    go_through = @neighbors.select {|v| v.flag==:opened}
    if go_through == nil
      return
    end
    go_through.each {|v| v.dfs}
    
  end
  
  def get_neighbor(id)
    tmp = @neighbors.select { |x| Integer(x.id)==Integer(id) }
    if(tmp==nil)
      return nil;
    else
      return tmp[0];
    end
      
  end
  
  def add_neighbor(node)
    if get_neighbor(node.id) == nil
      @neighbors = @neighbors << node
    end
  end
  
  def to_s
    out = "Node id: #{@id} flag: #{@flag} neighbors: "
    @neighbors.each { |n| out += n.id+" " }
    out += "\n"
    out
  end
end

class Solver
  def initialize(debug = false)
    @debug = debug;
    lines = []
      lines = STDIN.readlines
      analyze_lines(lines)
  end

  def analyze_lines(lines)
    @n = Integer(lines[0]);
    if @debug==true
      puts "Found #{@n} graphs!"
    end

    nOfLines = lines.length
    actualLine = 1

    for i in 1..@n
      n_of_nodes = Integer(lines[actualLine])
      graph = Graph.new();
      if @debug==true
        puts "Graph #{i} contains #{n_of_nodes} nodes";
      end
      actualLine += 1
      for j in 1..n_of_nodes
        numbers = lines[actualLine].split(' ')
        if @debug==true
          puts "Found properties for #{numbers[0]}-th node!";
        end
        node = graph.add_node(numbers[0])
        for j in 2..numbers.length-1
          neighbor = graph.add_node(numbers[j]);
          node.add_neighbor(neighbor);
        end
        actualLine += 1;
      end
      puts "graph #{i}"
      while lines[actualLine] != nil && lines[actualLine].include?(" ")
        numbers = lines[actualLine].split(' ')
        if Integer(numbers[0]) != 0
          if @debug==true
            puts "Found command #{numbers[0]} #{numbers[1]}!";
          end
          if Integer(numbers[1])==1
            graph.bfs(numbers[0])
          else
            graph.dfs(numbers[0])
          end
        end
        actualLine += 1
      end
    end
  end
end



s = Solver.new(false)
